namespace CIS
{
    using CISLib;
    using CISLib.UserControls;
    using System;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class UserCustomerSetPage4 : Page
    {
        protected DataGrid FromListDg;
        protected System.Web.UI.WebControls.Image iCustomerInfo;
        protected Label lbFromUser;
        protected Label lbToUser;
        protected DataGrid ToListDg;
        protected Button tranBtn;

        private void DgDataBind()
        {
            string strFromUserId = base.Request.QueryString["FromId"];
            string strToUserId = base.Request.QueryString["ToId"];
            this.FromListDg.DataSource = ManageCompany.GetCompanyInfoViaUserId(new Guid(strFromUserId));
            this.FromListDg.DataBind();
            this.ToListDg.DataSource = ManageCompany.GetCompanyInfoViaUserId(new Guid(strToUserId));
            this.ToListDg.DataBind();
        }

        protected Color GetFromColorViaCustomerId(object objCompanyId)
        {
            Guid userId = new Guid(base.Request.QueryString["FromId"]);
            SysUser user = ManageUser.GetUserViaId(userId);
            Province[] provinceAry = ManageProvince.GetProvinceAryViaUserId(userId);
            Cat[] catAry = ManageCAT.GetCATAryViaId(userId);
            Guid companyId = (Guid) objCompanyId;
            Company company = ManageCompany.GetCompanyViaId(companyId);
            bool isSameProvince = false;
            foreach (Province tmpP in provinceAry)
            {
                if (tmpP.ProvinceId.Equals(company.RegionId))
                {
                    isSameProvince = true;
                    break;
                }
            }
            bool isSameCat = false;
            foreach (Cat tmpC in catAry)
            {
                if (tmpC.CatId.Equals(company.CATtId))
                {
                    isSameCat = true;
                    break;
                }
            }
            if (isSameCat && isSameProvince)
            {
                return Color.Black;
            }
            return Color.Red;
        }

        protected Color GetToColorViaCustomerId(object objCompanyId)
        {
            Guid userId = new Guid(base.Request.QueryString["ToId"]);
            SysUser user = ManageUser.GetUserViaId(userId);
            Province[] provinceAry = ManageProvince.GetProvinceAryViaUserId(userId);
            Cat[] catAry = ManageCAT.GetCATAryViaId(userId);
            Guid companyId = (Guid) objCompanyId;
            Company company = ManageCompany.GetCompanyViaId(companyId);
            bool isSameProvince = false;
            foreach (Province tmpP in provinceAry)
            {
                if (tmpP.ProvinceId.Equals(company.RegionId))
                {
                    isSameProvince = true;
                    break;
                }
            }
            bool isSameCat = false;
            foreach (Cat tmpC in catAry)
            {
                if (tmpC.CatId.Equals(company.CATtId))
                {
                    isSameCat = true;
                    break;
                }
            }
            if (isSameCat && isSameProvince)
            {
                return Color.Black;
            }
            return Color.Red;
        }

        private void InitializeComponent()
        {
            this.tranBtn.Click += new EventHandler(this.tranBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            string strFromUserId = base.Request.QueryString["FromId"];
            string strToUserId = base.Request.QueryString["ToId"];
            SysUser fromUser = ManageUser.GetUserViaId(new Guid(strFromUserId));
            SysUser toUser = ManageUser.GetUserViaId(new Guid(strToUserId));
            this.lbFromUser.Text = fromUser.LoginName;
            this.lbToUser.Text = toUser.LoginName;
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.InitPage();
                this.DgDataBind();
            }
        }

        private void tranBtn_Click(object sender, EventArgs e)
        {
            string strToUserId = base.Request.QueryString["ToId"];
            foreach (DataGridItem item in this.FromListDg.Items)
            {
                RepeaterCheckBox isDelChkTmp = item.FindControl("isDelChk") as RepeaterCheckBox;
                if ((isDelChkTmp != null) && isDelChkTmp.Checked)
                {
                    Guid companyId = new Guid(isDelChkTmp.Value);
                    ManageCompany.UpdateCompanyEditorViaId(companyId, new Guid(strToUserId));
                }
            }
            this.DgDataBind();
        }
    }
}

